package com.leon.androidcoursedesign.ui.views;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

public class RoundPopupWindow extends PopupWindow {
    private final Context context;
    private final View anchor;

    private View popupView;

    public RoundPopupWindow(Context context, View anchor) {
        super(context);
        this.context = context;
        this.anchor = anchor;
    }

    /**
     * 设置popupWindow显示视图
     *
     * @param layoutId  layout ID
     * @return  返回this
     */
    public RoundPopupWindow setContentView(int layoutId) {
        popupView = LayoutInflater.from(context).inflate(layoutId, null);

        return this;
    }

    /**
     * 设置控件的监听事件
     *
     * @param viewId    监听的控件ID
     * @param onClickListener   需要覆盖View.OnClickListener
     * @return  返回this
     */
    public RoundPopupWindow setOnClickListener (int viewId, View.OnClickListener onClickListener) {
        popupView.findViewById(viewId).setOnClickListener(onClickListener);

        return this;
    }

    /**
     * 创建RoundPopupWindow对象
     */
    public void create() {
        this.setContentView(popupView);
        this.setHeight(ViewGroup.MarginLayoutParams.WRAP_CONTENT);
        this.setWidth(ViewGroup.MarginLayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setBackgroundDrawable(new ColorDrawable(0x00000000));
        this.showAsDropDown(anchor, 150, 0);
    }
}
